import java.io.*;
import java.util.*;

public class lines {
	public static int  count = 0;
	public static String[] extensions;

	/*
	 *	args[0] = directory name
	 *	args[1] = comma seperated file extenstions
	 */
	public static void main(String[] args){
		String base;
		if(args.length == 0)
			base = ".";
		else
			base = args[0];
		if(args.length == 2)
			extensions = args[1].split(",");
		else
			extensions = new String[]{ ".java", ".form", ".cpp", ".hpp" };
		try {
		File dir = new File(base);
		if(!dir.exists() || !dir.isDirectory())
			throw new Exception();
		StringBuilder sb = new StringBuilder("\n\n");
		sb.append(processDir(dir, "", sb)).append("------\n\nTotal: ").append("\n\n\n");
		System.out.println(sb.toString());
		} catch (Exception e) {
		}
	}

	public static int processDir(File dir, String path, StringBuilder sb){
		List<File> dirs = new ArrayList<File>();
		List<File> files = new ArrayList<File>();
		File[] contents = dir.listFiles();
		StringBuilder fsb = new StringBuilder();
		for(File f : contents){
			if(f.getName().length() > 2){
				if(f.isDirectory())
					dirs.add(f);
				else
					files.add(f);
			}
		}
		int total = 0;
		for(File f : files){
			int num = processFile(f);
			total += num;
			if(num > 0)
				fsb.append((path.equals("") ? dir.getName() + "/" : path)).append(f.getName()).append(": \t").append(num).append("\n");
		}
		if(total > 0)
			fsb.append("\n");
		for(File f : dirs)
			total += processDir(f,path + dir.getName() + "/", sb);
		if(total > 0)
			sb.append(fsb.toString()).append(path).append(dir.getName()).append(":\t").append(total).append("\n");
		return total;
	}
	public static boolean checkExtension(String filename){
		for(String ext : extensions)
			if(filename.endsWith(ext))
				return true;
		return false;
	}

	public static int processFile(File f){
		if(checkExtension(f.getName().toLowerCase()) && f.getName().indexOf("svn") < 0){
			try {
			Scanner scan = new Scanner(f);
			int count = 0;
			while (scan.hasNextLine()){
				scan.nextLine();
				count++;
			}
			return count;
			} catch (Exception e) { }
		} 
		return 0;
	}
}
